/*
 * Copyright (c) 2018-2999 广州市蓝海创新科技有限公司 All rights reserved.
 *
 * https://www.mall4j.com/
 *
 * 未经允许，不可做商业用途！
 *
 * 版权所有，侵权必究！
 */
package com.yami.shop.distribution.common.listener;

import com.yami.shop.bean.enums.OrderType;
import com.yami.shop.bean.event.OrderSettlementEvent;
import com.yami.shop.bean.order.ConfirmOrderOrder;
import com.yami.shop.distribution.common.service.DistributionUserIncomeService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.context.event.EventListener;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;


/**
 * 订单结算的事件
 * @author yami
 */
@Slf4j
@Component("orderSettlementDistributionListener")
@AllArgsConstructor
public class OrderSettlementListener {

    private final DistributionUserIncomeService distributionUserIncomeService;

    @EventListener(OrderSettlementEvent.class)
    @Order(ConfirmOrderOrder.DEFAULT)
    public void distributionSettlementOrderEvent(OrderSettlementEvent event) {
        List<String> orderNumbers = new ArrayList<>();
        for (com.yami.shop.bean.model.Order order : event.getOrder()) {
            //如果是积分订单无需操作
            if (Objects.equals(order.getOrderType(), OrderType.SCORE.value())) {
                continue;
            }
            orderNumbers.add(order.getOrderNumber());
        }
        if(CollectionUtils.isNotEmpty(orderNumbers)) {
            distributionUserIncomeService.commissionSettlementHandle(orderNumbers);
        }
    }

}
